package lesson01;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/*    服务端
      1.建立服务器 ServerSocket serverSocket=new ServerSoket(prot:9999);
      2.等待客户端连接  socket=ServerSocket.accept();
      3.读取用户端的消息      InpurStream is=null;
                           is=socket.getIputStream();
*/
public class TcpServerDemo01 {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket=null;
        Socket socket=null;
        InputStream is=null;
        ByteArrayOutputStream baos=null;
        while(true)
        try {//1.我得有一个地址
            serverSocket=new ServerSocket(9999);
             //2.等待客户端连接过来
           socket=serverSocket.accept();
             //3.读取客户端的消息
            is =socket.getInputStream();

            //管道流
            baos=new ByteArrayOutputStream();
            byte[] buffer=new byte[1024];
            int len;
            while((len=is.read(buffer))!=-1)
            {
                baos.write(buffer,0,len);
            }
            System.out.println(baos.toString());

        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(baos!=null)
            { baos.close();
            }
            if(is!=null)
            { is.close();
            }
            if(socket!=null)
            { socket.close();
            }
            if(serverSocket!=null)
            { serverSocket.close();
            }

        }
    }
}
